SetMaxIdleConns && SetMaxIdleConns

Go 的 sql 包中 DB struct SetMaxIdleConns 与 SetMaxOpenConns 存在意义

1
2
3
4
5
6
7
8
9
// SetMaxOpenConns sets the maximum number of open connections to the database.
//
// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
// MaxIdleConns, then MaxIdleConns will be reduced to match the new
// MaxOpenConns limit.
//
// If n <= 0, then there is no limit on the number of open connections.
// The default is 0 (unlimited).
func (db *DB) SetMaxOpenConns(n int) {}

1
2
3
4
5
6
7
8
9
10
11
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool.
//
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
// then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
//
// If n <= 0, no idle connections are retained.
//
// The default max idle connections is currently 2. This may change in
// a future release.
func (db *DB) SetMaxIdleConns(n int) {}

传送门

以免大家打不开传送门

SetMaxIdleConns 设置数据库连接池可以保留的连接数,用来复用连接。
SetMaxOpenConns 设置数据库可以打开的最大连接数。
意义:因为你可能希望MaxIdleConns是低的,但是MaxOpenConns是高的。这意味着您的应用程序可以在需要的时候打开尽可能多的连接,但在不需要的时候却消耗最少的资源:适合于突发负载。–Danver Braganza 2015年8月11日22:55